<?php
	class AdmDiplome extends AdmModule {
		public function action_index() {
			if (isset($this->session->modification_diplome_id)) {
				unset($this->session->modification_diplome_id);
			}
		
			$this->set_title('Liste des diplomes');
			$this->tpl->assign('liste', Diplome::recupererListe());
		}
		
		public function action_ajouter() {
			$formulaire = new Form("?module=admdiplome&action=valider_ajout","formulaire");
			$formulaire->add_text("diplome","diplome","Nom du diplome :");
			$formulaire->add_submit("ajouter_diplome_submit", "ajouter_diplome_submit")->set_value("Ajouter le diplome");	
			
			$this->tpl->assign("formulaire", $formulaire);		
		}
		
		public function action_valider_ajout() {
			if ($this->req->diplome == '') {
				$this->site->ajouter_message("Veuillez indiquer un nom de diplome.");
				$this->site->redirect('admdiplome', 'ajouter');
			}
			else {
				$diplome = new Diplome($this->req->diplome);
				$diplome->enregistrer();
				
				$this->site->redirect('admdiplome');
			}
		}
		
		public function action_modifier() {
			if ($this->req->id > 0 || $this->session->modification_diplome_id > 0) {
				try {
					$diplome = Diplome::recupererAvecID($this->req->id);
				}
				catch (MyException $e) {
					try {
						$diplome = Diplome::recupererAvecID($this->session->modification_diplome_id);
					}
					catch (MyException $e) {
					}
				}
				
				if (isset($this->session->modification_diplome_id)) {
					$this->req->id = $this->session->modification_diplome_id;
					unset($this->session->modification_diplome_id);
				}
			
				$formulaire = new Form("?module=admdiplome&action=valider_modification","formulaire");
				$formulaire->add_text("diplome","diplome","Nom du diplome :")->set_value($diplome->valeur);
				$formulaire->add_submit("modifier_diplome_submit", "modifier_diplome_submit")->set_value("Modifier le diplome");	
				$this->session->modification_diplome_id = $this->req->id;
				
				$this->tpl->assign("formulaire", $formulaire);		
			}
			else {
				$this->site->redirect('admdiplome');
			}
		}
		
		public function action_valider_modification() {
			if ($this->session->modification_diplome_id <= 0) {
				$this->site->redirect('admdiplome');
			}
			elseif ($this->req->diplome == '') {
				$this->site->ajouter_message('Vous ne pouvez pas donner un nom vide !');
				$this->site->redirect('admdiplome', 'modifier');
			}	
			else {
				unset($this->req->modification_diplome_id);
				
				$diplome = Diplome::recupererAvecID($this->session->modification_diplome_id);
				$diplome->valeur = $this->req->diplome;
				
				$diplome->enregistrer();
				
				$this->site->ajouter_message('Modification effectuée');
				$this->site->redirect('admdiplome');
			}
		}
		
		public function action_supprimer() {
			if ($this->req->id > 0) {
				$diplome = Diplome::recupererAvecID($this->req->id);
				$diplome->supprimer();
				
				$this->site->ajouter_message('Suppression effectuée');
				$this->site->redirect('admdiplome');
			}
			else {
				$this->site->redirect('admdiplome');
			}
		}
	}
?>